﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using H2.NET.Common;
using H2.NET.Data.Common.Validates;
using H2.NET.Web.Workflow.DwzCode;
using Ultimus.OC;
using Ultimus.WFServer;
using UServLib;

namespace H2.NET.Web.Workflow.Workflow
{
    public partial class PersonalInfo : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var action = AksRequest.GetQueryStringValue("action");
            if (string.IsNullOrEmpty(action)) //默认页面加载
            {
                var currentUser = new User();
                currentUser.strUserName = clsCmm.comm_username;

                UserPreferences currentUserP;
                if (currentUser.GetUserPrefs(out currentUserP))
                {
                    txtEmail.Text = currentUserP.strEmail;
                    if (currentUserP.AssociatesList != null)
                    {
                        //同事列表
                        var dtAssociates = new DataTable();
                        dtAssociates.Columns.Add("strUserName");
                        dtAssociates.Columns.Add("strUserFullName");
                        foreach (var user in currentUserP.AssociatesList)
                        {
                            var row = dtAssociates.NewRow();
                            row["strUserName"] = user.strUserName;
                            row["strUserFullName"] = user.strUserFullName;

                            dtAssociates.Rows.Add(row);
                        }

                        gvAssociates.DataSource = dtAssociates;
                        gvAssociates.DataBind();
                    }
                }
            }
            else if (action == "add_to_associates") //form提交
            {
                AddToAssociates();
            }
            else if (action == "delete_associates") //form提交
            {
                DeleteAssociates();
            }
        }

        public void AddToAssociates()
        {
            var currentOc = new OrgChart();
            var currentUser = new User();
            UserPreferences currentUserP;
            currentUser.strUserName = clsCmm.comm_username;

            //如果存在返回值的情况
            if (currentUser.GetUserPrefs(out currentUserP))
            {
                //邮箱
                currentUserP.strEmail = AksRequest.GetString("txtEmail");

                var associates = new List<User>();
                if (currentUserP.AssociatesList != null)
                {
                    associates.AddRange(currentUserP.AssociatesList);
                }

                //新加入的同事
                User newUser;
                currentOc.FindUser(AksRequest.GetString("associate"), "", 0, out newUser);
                if (newUser != null)
                {
                    associates.Add(newUser);
                }
                currentUserP.AssociatesList = associates.ToArray();

                //执行“设置同事”具体的保存操作    
                if (currentUser.SetUserPrefs(currentUserP))
                {
                    //将同事加入临时表
                    clsCmm.clsDB.Storage("oleDB");
                    clsCmm.clsDB.ExcuteSql("insert into t_Associate(ass_name, ass_associate, ass_associate_name)values('" + clsCmm.comm_username + "','" + newUser.strUserName + "','" + newUser.strUserFullName + "')");

                    WriteToClient("true", "text/plain");
                }
                else
                {
                    WriteToClient("false", "text/plain");
                }

            }
        }

        public void DeleteAssociates()
        {
            var currentOc = new OrgChart();
            var currentUser = new User();
            UserPreferences currentUserP;
            currentUser.strUserName = clsCmm.comm_username;

            //如果存在返回值的情况
            if (currentUser.GetUserPrefs(out currentUserP))
            {
                var associates = new List<User>();
                var associateDelete = AksRequest.GetString("associate");
                if (currentUserP.AssociatesList != null)
                {
                    foreach (var user in currentUserP.AssociatesList)
                    {
                        if (user.strUserName != associateDelete) //去除要删除的用户
                        {
                            associates.Add(user);
                        }
                    }
                }
                currentUserP.AssociatesList = associates.ToArray();

                //执行“设置同事”具体的保存操作    
                if (currentUser.SetUserPrefs(currentUserP))
                {
                    //将同事从临时表中移除
                    clsCmm.clsDB.Storage("oleDB");
                    clsCmm.clsDB.ExcuteSql("delete from t_Associate where ass_name='" + clsCmm.comm_username + "' and ass_associate='" + associateDelete + "'");

                    WriteToClient("true", "text/plain");
                }
                else
                {
                    WriteToClient("false", "text/plain");
                }

            }
        }


    }
}